/*   RolGPS Proyect
 *   Copyright (C) 2009 �lvaro Dur�n Tovar <hermesdt@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package irclientplugin;

import java.io.*;
import java.util.Vector;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;
import org.jdom.filter.Filter;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author alvaro
 */
public class XML {
    
    private Document doc;
    
    /** Creates a new instance of Configuracion */
    public XML(String xmlFile) {
        Document doc = null;
        try{
            doc = readXML(xmlFile);
        }catch(Exception e){
            //javax.swing.JOptionPane.showMessageDialog(null, "Hubo un error, la aplicacion debe cerrarse", null, javax.swing.JOptionPane.ERROR_MESSAGE);
            System.out.println("[XMLPluginConfiguration.Constructor]");
            e.printStackTrace();
            //System.exit(0);
        }
        this.doc = doc;
    }
    
    public void dumpFile(String file){
        OutputStream os = null;
        try{
             os = new FileOutputStream(new File(file));
        }catch(Exception e){
            e.printStackTrace();
            return;
        }
        
        XMLOutputter xml = new XMLOutputter();
        
        Format form = Format.getPrettyFormat();
        form.setExpandEmptyElements(true);
        form.setEncoding("ISO-8859-1");
        form.setIndent("\t");
        form.setLineSeparator("\n");
        xml.setFormat(form);
        
        try{
            xml.output(doc, os);
            os.close();
        }catch(Exception e){
            System.err.println("[XMLPluginConfiguration.dumpFile]");
            e.printStackTrace();
        }
    }
    
    public void removePluginNode(String pluginName){
        Element root = (Element) doc.getRootElement();
        List childs = root.getChildren();
        for(int i = 0;i<childs.size();i++){
            if(((Element) childs.get(i)).getAttributeValue("name").equals(pluginName)){
                childs.remove(i);
                break;
            }
        }
        
    }
    
    public Document readXML(String filePath) throws JDOMException, IOException {

        File f = new File(filePath);
        SAXBuilder b = new SAXBuilder(false);

        return b.build(f);
    }
    
    public List selectNodes(Document doc, String xPath){
        List list = null;
        XPath xPathProc = null;
        try{
            xPathProc = XPath.newInstance(xPath);
            list = xPathProc.selectNodes(doc);
        }catch(Exception e){
            
            e.printStackTrace();
            //System.exit(0);
        }
        return list;
    }
    
    String getJarFileName(){
        List aux = selectNodes(doc, "/program/jar_file_name");
        Element el = (Element) aux.get(0);
        return el.getText();
    }
    
    String getModule(){
        List aux = selectNodes(doc, "/program/module_name");
        Element el = (Element) aux.get(0);
        return el.getText();
    }
    
    public String getVersion(){
        List aux = selectNodes(doc, "/program/version");
        Element el = (Element) aux.get(0);
        return el.getText().trim();
    }
    
    public String getTag(String tag){
        List aux = selectNodes(doc, tag);
        if(aux.size() == 0)
            return null;
        Element el = (Element) aux.get(0);
        
        return el.getText();
    }
    
    String[] getLibraries(){
        List aux = selectNodes(doc, "/program/libraries");
        Element el = (Element) aux.get(0);
        String libs[] = new String[el.getChildren().size()];
        
        for(int i = 0;i<libs.length;i++){
            libs[i] = ((Element) el.getChildren().get(i)).getText();
        }
        
        return libs;
    }

    public Document getDoc() {
        return doc;
    }
}
